package com.chhuang.resolver;

import cn.dev33.satoken.stp.StpUtil;
import com.chhuang.constants.MConstants;
import com.chhuang.bean.SaUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @Description sa-user-session中用户信息解析器
 * @Author Darren Huang
 * @Date 2022-12-22 8:41
 */
@Slf4j
public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver {
    /**
     * 入参筛选
     * @param parameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(SaUser.class);
    }

    /**
     * 出参设置
     * @param parameter
     * @param mavContainer
     * @param webRequest
     * @param binderFactory
     * @return
     * @throws Exception
     */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
        return StpUtil.getSession().getModel(MConstants.SESSION_USER_KEY, SaUser.class);
    }
}
